package cn.javass.framework.web.core;

import java.util.HashMap;
import java.util.Map;

import com.sun.org.apache.regexp.internal.RE;

public class ActionContext {
	private static String ACTION = "action";
	private static String SESSION = "session";
	private static String APPLICATION = "application";
	private static ThreadLocal actionContext= new ContextThreadLocal();
	private Map context;
	public ActionContext(Map context) {
		this.context = context;
	}
	
	 public void setAction(Object action) {
	        put(ACTION, action);
	    }

	    public Object getAction() {
	        return get(ACTION);
	    }
	
    public void setApplication(Map application) {
        put(APPLICATION, application);
    }

    public Map getApplication() {
        return (Map) get(APPLICATION);
    }
	
	public static void setContext(ActionContext context) {
        actionContext.set(context);
    }
	
    public static ActionContext getContext() {
        ActionContext context = (ActionContext) actionContext.get();

        if (context == null) {
            context = new ActionContext(new HashMap());
            setContext(context);
        }

        return context;
    }
    public void setSession(Map session) {
        put(SESSION, session);
    }

    public Map getSession() {
        return (Map) get(SESSION);
    }
	
	
	public void put(Object key,Object value){
		context.put(key, value);
	}
	
	public Object get(Object key){
		return context.get(key);
	}
	private static class ContextThreadLocal extends ThreadLocal{
		@Override
		protected Object initialValue() {
			return new ActionContext(new HashMap());
		}
	}
	public static void main(String[] args) {
		Map aa = new HashMap();
		aa.put("a", "bbbbbb");
		ActionContext.getContext().put("a", aa);
		aa.put("a", "ccccc");
		System.out.println(ActionContext.getContext().get("a"));
	}
}
